import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // JSON.parse(localStorage.getItem('cartlist')) || 
    cartList: []
  },
  mutations: {
    add(state,goods){
      let rel = true
      state.cartList.map(item=>{
        console.log(item.goods.spuId)
        if(item.goods.spuId == goods.spuId){
          item.num++
          rel = false
        }
      })
      if(rel){
        state.cartList.push({goods,num:1})
      }
      // localStorage.setItem('cartlist',JSON.stringify(state.cartlist))
    },
    sub(state,val){
      state.cartList.map(item=>{
        if(item.goods.id == val.id){
          item.num--
          if(item.num == 0){
            let k = state.cartList.indexOf(item)
            state.cartList.splice(k,1)
          }
        }
      })
      // localStorage.setItem('cartlist',JSON.stringify(state.cartlist))
    }
  },
  actions: {
  },
  modules: {
  }
})
